<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="数据集名称"
            prop="name"
            :rules="[{ required: true, message: '请输入数据集名称', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="数据来源"
            prop="sjlx"
            :rules="[{ required: true, message: '请选择数据来源', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.sjlx" clearable placeholder="请选择数据来源" @change="dataChange">
              <el-option
                v-for="item in dataTypes"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="数据项"
            prop="sjx"
            :rules="[{ required: true, message: '请选择数据项', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.sjx" clearable placeholder="请选择数据项" @change="changeObject">
              <el-option
                v-for="item in dataList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否全局数据"
            prop="sfqj"
            :rules="{ required: true, message: '请选择是否全局数据', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.sfqj" style="width: 200px">
              <el-radio
                v-for="item in guest"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateObject, saveObject } from '@/api/portal/manager/data/index'
import { queryAllSystemDatasource, querySjlx } from '@/api/portal/manager/setting'
import { queryAllSjx } from '@/api/mobile/data'
export default {
  name: 'DbTaskManagerEntity',
  data() {
    return {
      title: '数据项管理',
      loading: false,
      dialogFormVisible: false,
      dataTypes: [],
      dataList: [],
      guest: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }],
      form: {
        id: null,
        name: '',
        sjlx: null,
        sjx: null,
        jxx: '',
        sfqj: false,
        px: 1
      }
    }
  },
  methods: {
    openEntity(data) {
      this.queryData = null
      this.initDataType()
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.title = '修改信息'
        this.dialogFormVisible = true
        this.dataChange()
      } else {
        this.title = '新增信息'
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    dataChange() {
      if (this.form.sjlx !== null && this.form.sjlx !== '') {
        if (this.form.sjlx === 1) {
          this.initDataSource()
        } else if (this.form.sjlx === 2) {
          this.initSystemData()
        }
      } else {
        this.form.sjx = null
      }
    },
    changeObject(obj) {
      const data = this.dataList.filter(item => {
        if (item.id === obj) {
          return item
        }
      })
      this.form.jxx = data[0].jxx
    },
    initDataType() {
      querySjlx().then(res => {
        const { data } = res
        this.dataTypes = data
      })
    },
    initDataSource() {
      queryAllSjx().then(res => {
        const { data } = res
        if (undefined !== data && data !== null) {
          this.dataList = data.data
        }
      })
    },
    initSystemData() {
      queryAllSystemDatasource().then(res => {
        const { data } = res
        this.dataList = data
      })
    }
  }
}
</script>

<style scoped>

</style>
